package io.xdag.block;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BlockPool  implements Runnable {
    
    private static final Logger log = LoggerFactory.getLogger(BlockPool.class);

    private ExecutorService blockPool;
    
    public boolean shutdown = false;

    public final static int PORT_BYTES = 10;

    public BlockPool() {
    }

    @Override
    public void run() {
        
    	blockPool = Executors.newFixedThreadPool(XBlock.NUM_THREADS);
        {           
        	createSink();
        }
        
        while (!Thread.interrupted()) {
            // Restart attempt for neighbors that are in the configuration.
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                log.debug("Interrupted: ", e);
            }
            createSink();
        }
    }
    
    public void createSink() {
        Runnable proc = new BlockProcessor();
        blockPool.submit(proc);
    }


    public void shutdown() throws InterruptedException {
        shutdown = true;
        blockPool.shutdown();
        blockPool.awaitTermination(6, TimeUnit.SECONDS);
    }
}
